﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Printing;

namespace Admin.业务界面
{
    public partial class Result : Form
    {
        //List<StuGroup> stu_Groups = new List<StuGroup>();
        public Result()
        {
            InitializeComponent();
        }

        private void Result_Load(object sender, EventArgs e)
        {
            BLL.B_Group b_Group = new BLL.B_Group();
            dataGridView1.DataSource = b_Group.GetList();
            //dataGridView1.DataSource = stu_Groups;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // 配置打印对话框
            printDialog1.Document = printDocument1;

            // 显示打印对话框
            if (printDialog1.ShowDialog() == DialogResult.OK)
            {
                // 用户点击了打印，显示打印预览
                printPreviewDialog1.Document = printDocument1;
                printPreviewDialog1.ShowDialog();
            }
        }

        private int currentRow = 0;
        private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
        {
            /// 设置字体
            Font font = new Font("Arial", 20);
            Font headerFont = new Font("Arial", 20, FontStyle.Bold); // 表头使用的字体，可以是更大或者加粗
            // 列间距
            int columnSpacing = 80;
            int startX = 50;
            int startY = 100;
            int offset = 40;         


            // 指定要打印的列索引
            List<int> columnsToPrint = new List<int> { 1,9 }; // 假设你只想打印第2、10列

            // 设置标题
            if (currentRow == 0)
            {
                e.Graphics.DrawString("打印预览", new Font("Arial", 16), Brushes.Black, startX, startY - offset);
                float headerY = startY;
                foreach (int columnIndex in columnsToPrint)
                {
                    // 获取列对象
                    DataGridViewColumn column = dataGridView1.Columns[columnIndex];
                    // 打印列名
                    e.Graphics.DrawString(column.HeaderText, headerFont, Brushes.Black, startX, headerY);
                    // 调整下一列的X坐标
                    startX += column.Width + columnSpacing;
                }
                // 调整数据行的起始Y坐标
                startY += headerFont.Height + offset;
            }

            // 重置X坐标开始打印数据
            startX = 50;

            while (currentRow < dataGridView1.Rows.Count)
            {
                DataGridViewRow row = dataGridView1.Rows[currentRow];
                // 获取每个你想打印的单元格
                string column1 = row.Cells["group_name"].Value?.ToString(); 
                string column2 = row.Cells["teacher_name"].Value?.ToString(); 
                // ... 根据实际情况获取更多列

                // 测量字符串所需的空间
                SizeF column1Size = e.Graphics.MeasureString(column1, font);
                SizeF column2Size = e.Graphics.MeasureString(column2, font);
                // ... 对更多列进行测量

                // 打印每列
                e.Graphics.DrawString(column1, font, Brushes.Black, startX, startY);
                e.Graphics.DrawString(column2, font, Brushes.Black, startX + column1Size.Width + columnSpacing, startY);
                // ... 继续绘制更多列

                startY += (int)Math.Max(column1Size.Height, column2Size.Height) + 30;
                //column1Size.Height 和 column2Size.Height 表示当前行中最高的单元格的高度（如果有多个单元格，需要确定哪个单元格是最高的）。
                //这个最大高度确保了文本不会相互重叠。紧接着，+ 5 是额外增加的间距值。


                // 超过页面，准备下一页
                if (startY > e.MarginBounds.Bottom)
                {
                    e.HasMorePages = true;
                    currentRow++;
                    break;
                }

                currentRow++;
                e.HasMorePages = false;
            }
        }
    }
}
